package gui;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.*;
/*
 * Created by JFormDesigner on Wed Jan 25 12:03:44 CET 2012
 */

import service.EncService;

/**
 * @author Sebastian Dietrich
 */
public class MainFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MainFrame() {
		initComponents();
		service = new EncService();
	}

	private void fileChoButtonActionPerformed(ActionEvent e) {
		JFileChooser fc = new JFileChooser();
		fc.setMultiSelectionEnabled(false);
		fc.setFileHidingEnabled(true);
		int selectedOption = fc.showOpenDialog(this);
		if (selectedOption == JFileChooser.APPROVE_OPTION) {
			File selectedFile = fc.getSelectedFile();
			fileTextField.setText(selectedFile.getAbsolutePath());
			url = selectedFile.getAbsolutePath();
		}
	}

	private void encButtonActionPerformed(ActionEvent e) {
		File f = null;
		try {
			f = openFileFromFileTextField();
		} catch (FileNotFoundException e1) {
			JOptionPane.showMessageDialog(this, "Error! The chosen file (" + fileTextField.getText() + ") is not valid. \nCheck the path and if the file exists.", "File not found", JOptionPane.WARNING_MESSAGE);
			return;
		}
		try {
			url = service.encode(f);
			fileTextField.setText(url);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	private File openFileFromFileTextField() throws FileNotFoundException {
		File f = new File(fileTextField.getText());
		if (!f.isFile()) {
			throw new FileNotFoundException();
		}
		return f;
	}

	private void decButtonActionPerformed(ActionEvent e) {
		File f = null;
		try {
			f = openFileFromFileTextField();
		} catch (FileNotFoundException e1) {
			JOptionPane.showMessageDialog(this, "Error! The chosen file (" + fileTextField.getText() + ") is not valid. \nCheck the path and if the file exists.", "File not found", JOptionPane.WARNING_MESSAGE);
			return;
		}
		try {
			url = service.decode(f);
			fileTextField.setText(url);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	private void openButtonActionPerformed(ActionEvent e) {
		if(url != null) {
			try {
				new ProcessBuilder("explorer.exe", "/select,\"" + url + "\"").start();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		encryptionPanel = new JPanel();
		headinLabel = new JLabel();
		fileTextField = new JTextField();
		fileChoButton = new JButton();
		encButton = new JButton();
		decButton = new JButton();
		openButton = new JButton();

		//======== this ========
		setTitle("Encrypting by Martin Lemanski");
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== encryptionPanel ========
		{
			encryptionPanel.setLayout(new GridBagLayout());
			((GridBagLayout)encryptionPanel.getLayout()).columnWidths = new int[] {13, 26, 26, 26, 23, 28, 27, 27, 25, 26, 0, 0};
			((GridBagLayout)encryptionPanel.getLayout()).rowHeights = new int[] {12, 0, 0, 0, 0, 0, 0};
			((GridBagLayout)encryptionPanel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
			((GridBagLayout)encryptionPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

			//---- headinLabel ----
			headinLabel.setText("Choose a File");
			encryptionPanel.add(headinLabel, new GridBagConstraints(1, 1, 9, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 5), 0, 0));
			encryptionPanel.add(fileTextField, new GridBagConstraints(1, 2, 9, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 5), 0, 0));

			//---- fileChoButton ----
			fileChoButton.setText("...");
			fileChoButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					fileChoButtonActionPerformed(e);
				}
			});
			encryptionPanel.add(fileChoButton, new GridBagConstraints(10, 2, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 0), 0, 0));

			//---- encButton ----
			encButton.setText("encrypt");
			encButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					encButtonActionPerformed(e);
				}
			});
			encryptionPanel.add(encButton, new GridBagConstraints(1, 3, 9, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 5), 0, 0));

			//---- decButton ----
			decButton.setText("decrypt");
			decButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					decButtonActionPerformed(e);
				}
			});
			encryptionPanel.add(decButton, new GridBagConstraints(1, 4, 9, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 5), 0, 0));

			//---- openButton ----
			openButton.setText("open Explorer");
			openButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openButtonActionPerformed(e);
				}
			});
			encryptionPanel.add(openButton, new GridBagConstraints(1, 5, 9, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 0, 5), 0, 0));
		}
		contentPane.add(encryptionPanel, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(getOwner());
		// //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	private JPanel encryptionPanel;
	private JLabel headinLabel;
	private JTextField fileTextField;
	private JButton fileChoButton;
	private JButton encButton;
	private JButton decButton;
	private JButton openButton;
	// JFormDesigner - End of variables declaration //GEN-END:variables
	private EncService service;
	private String url;
}
